package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.common.thriftserver.ApiRequest;

/**
 * Thrift 测试工具
 * @author whq-mac
 *
 */

@Controller
@Scope("prototype")
public class IceTestController {
//	private static final Logger LOGGER = LoggerFactory.getLogger(IceTestController.class);
	
	@RequestMapping(value="bs/icetest/index")
	public String index(
			@RequestParam(value="ipAddress", required=false) String ipAddress,
	        @RequestParam(value="port", required=false) String port,
	        
	        @RequestParam(value="ctxParamName_1", required=false) String ctxParamName_1,
	        @RequestParam(value="ctxParamName_2", required=false) String ctxParamName_2,
	        @RequestParam(value="ctxParamName_3", required=false) String ctxParamName_3,
	        
	        @RequestParam(value="ctxParamVals_1", required=false) String ctxParamVals_1,
	        @RequestParam(value="ctxParamVals_2", required=false) String ctxParamVals_2,
	        @RequestParam(value="ctxParamVals_3", required=false) String ctxParamVals_3,
	        
	        @RequestParam(value="paramName_1", required=false) String paramName_1,
	        @RequestParam(value="paramName_2", required=false) String paramName_2,
	        @RequestParam(value="paramName_3", required=false) String paramName_3,
	        @RequestParam(value="paramName_4", required=false) String paramName_4,
	        @RequestParam(value="paramName_5", required=false) String paramName_5,
	        @RequestParam(value="paramName_6", required=false) String paramName_6,
	        @RequestParam(value="paramName_7", required=false) String paramName_7,
	        @RequestParam(value="paramName_8", required=false) String paramName_8,
	        @RequestParam(value="paramName_9", required=false) String paramName_9,
	        @RequestParam(value="paramName_10", required=false) String paramName_10,
	        @RequestParam(value="paramName_11", required=false) String paramName_11,
	        @RequestParam(value="paramName_12", required=false) String paramName_12,
	        @RequestParam(value="paramName_13", required=false) String paramName_13,
	        @RequestParam(value="paramName_14", required=false) String paramName_14,
	        @RequestParam(value="paramName_15", required=false) String paramName_15,
	        @RequestParam(value="paramName_16", required=false) String paramName_16,
	        @RequestParam(value="paramName_17", required=false) String paramName_17,
	        @RequestParam(value="paramName_18", required=false) String paramName_18,
	        @RequestParam(value="paramName_19", required=false) String paramName_19,
	        @RequestParam(value="paramName_20", required=false) String paramName_20,
	        @RequestParam(value="paramName_21", required=false) String paramName_21,
	        @RequestParam(value="paramName_22", required=false) String paramName_22,
	        @RequestParam(value="paramName_23", required=false) String paramName_23,
	        @RequestParam(value="paramName_24", required=false) String paramName_24,
	        @RequestParam(value="paramName_25", required=false) String paramName_25,
	        
	        @RequestParam(value="paramVal_1", required=false) String paramVal_1,
	        @RequestParam(value="paramVal_2", required=false) String paramVal_2,
	        @RequestParam(value="paramVal_3", required=false) String paramVal_3,
	        @RequestParam(value="paramVal_4", required=false) String paramVal_4,
	        @RequestParam(value="paramVal_5", required=false) String paramVal_5,
	        @RequestParam(value="paramVal_6", required=false) String paramVal_6,
	        @RequestParam(value="paramVal_7", required=false) String paramVal_7,
	        @RequestParam(value="paramVal_8", required=false) String paramVal_8,
	        @RequestParam(value="paramVal_9", required=false) String paramVal_9,
	        @RequestParam(value="paramVal_10", required=false) String paramVal_10,
	        @RequestParam(value="paramVal_11", required=false) String paramVal_11,
	        @RequestParam(value="paramVal_12", required=false) String paramVal_12,
	        @RequestParam(value="paramVal_13", required=false) String paramVal_13,
	        @RequestParam(value="paramVal_14", required=false) String paramVal_14,
	        @RequestParam(value="paramVal_15", required=false) String paramVal_15,
	        @RequestParam(value="paramVal_16", required=false) String paramVal_16,
	        @RequestParam(value="paramVal_17", required=false) String paramVal_17,
	        @RequestParam(value="paramVal_18", required=false) String paramVal_18,
	        @RequestParam(value="paramVal_19", required=false) String paramVal_19,
	        @RequestParam(value="paramVal_20", required=false) String paramVal_20,
	        @RequestParam(value="paramVal_21", required=false) String paramVal_21,
	        @RequestParam(value="paramVal_22", required=false) String paramVal_22,
	        @RequestParam(value="paramVal_23", required=false) String paramVal_23,
	        @RequestParam(value="paramVal_24", required=false) String paramVal_24,
	        @RequestParam(value="paramVal_25", required=false) String paramVal_25,
	        
	        @RequestParam(value="name", required=false) String name,
	        Model model,
	        HttpServletRequest request) {
		model.addAttribute("ipAddress", ipAddress);
		model.addAttribute("port", port);
		
		model.addAttribute("ctxParamName_1", ctxParamName_1);
		model.addAttribute("ctxParamName_2", ctxParamName_2);
		model.addAttribute("ctxParamName_3", ctxParamName_3);
		
		model.addAttribute("ctxParamVals_1", ctxParamVals_1);
		model.addAttribute("ctxParamVals_2", ctxParamVals_2);
		model.addAttribute("ctxParamVals_3", ctxParamVals_3);
		
		model.addAttribute("paramName_1", paramName_1);
		model.addAttribute("paramName_2", paramName_2);
		model.addAttribute("paramName_3", paramName_3);
		model.addAttribute("paramName_4", paramName_4);
		model.addAttribute("paramName_5", paramName_5);
		model.addAttribute("paramName_6", paramName_6);
		model.addAttribute("paramName_7", paramName_7);
		model.addAttribute("paramName_8", paramName_8);
		model.addAttribute("paramName_9", paramName_9);
		model.addAttribute("paramName_10", paramName_10);
		model.addAttribute("paramName_11", paramName_11);
		model.addAttribute("paramName_12", paramName_12);
		model.addAttribute("paramName_13", paramName_13);
		model.addAttribute("paramName_14", paramName_14);
		model.addAttribute("paramName_15", paramName_15);
		model.addAttribute("paramName_16", paramName_16);
		model.addAttribute("paramName_17", paramName_17);
		model.addAttribute("paramName_18", paramName_18);
		model.addAttribute("paramName_19", paramName_19);
		model.addAttribute("paramName_20", paramName_20);
		model.addAttribute("paramName_21", paramName_21);
		model.addAttribute("paramName_22", paramName_22);
		model.addAttribute("paramName_23", paramName_23);
		model.addAttribute("paramName_24", paramName_24);
		model.addAttribute("paramName_25", paramName_25);
		
		model.addAttribute("paramVal_1", paramVal_1);
		model.addAttribute("paramVal_2", paramVal_2);
		model.addAttribute("paramVal_3", paramVal_3);
		model.addAttribute("paramVal_4", paramVal_4);
		model.addAttribute("paramVal_5", paramVal_5);
		model.addAttribute("paramVal_6", paramVal_6);
		model.addAttribute("paramVal_7", paramVal_7);
		model.addAttribute("paramVal_8", paramVal_8);
		model.addAttribute("paramVal_9", paramVal_9);
		model.addAttribute("paramVal_10", paramVal_10);
		model.addAttribute("paramVal_11", paramVal_11);
		model.addAttribute("paramVal_12", paramVal_12);
		model.addAttribute("paramVal_13", paramVal_13);
		model.addAttribute("paramVal_14", paramVal_14);
		model.addAttribute("paramVal_15", paramVal_15);
		model.addAttribute("paramVal_16", paramVal_16);
		model.addAttribute("paramVal_17", paramVal_17);
		model.addAttribute("paramVal_18", paramVal_18);
		model.addAttribute("paramVal_19", paramVal_19);
		model.addAttribute("paramVal_20", paramVal_20);
		model.addAttribute("paramVal_21", paramVal_21);
		model.addAttribute("paramVal_22", paramVal_22);
		model.addAttribute("paramVal_23", paramVal_23);
		model.addAttribute("paramVal_24", paramVal_24);
		model.addAttribute("paramVal_25", paramVal_25);
		
		model.addAttribute("name", name);
		
		String iceResponse = "";
		if (StringUtil.isBlank(ipAddress) 
				|| StringUtil.isBlank(port)
				) {
			model.addAttribute("iceResponse", iceResponse);
			return "bs/icetest/index";
		}
		
		Map<String, String> ctxMap = new HashMap<String, String>(0);
		ctxMap.put(ctxParamName_1, ctxParamVals_1);
		ctxMap.put(ctxParamName_2, ctxParamVals_2);
		ctxMap.put(ctxParamName_3, ctxParamVals_3);
		
		for (Iterator<Map.Entry<String, String>> iterator = ctxMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			String paramName = entry.getKey();
			String paramVal = entry.getValue();
			if (StringUtil.isBlank(paramName) 
					|| StringUtil.isBlank(paramVal)
					) {
				iterator.remove();
			}
		}
		
		Map<String, String> paramMap = new HashMap<String, String>(0);
		paramMap.put(paramName_1, paramVal_1);
		paramMap.put(paramName_2, paramVal_2);
		paramMap.put(paramName_3, paramVal_3);
		paramMap.put(paramName_4, paramVal_4);
		paramMap.put(paramName_5, paramVal_5);
		paramMap.put(paramName_6, paramVal_6);
		paramMap.put(paramName_7, paramVal_7);
		paramMap.put(paramName_8, paramVal_8);
		paramMap.put(paramName_9, paramVal_9);
		paramMap.put(paramName_10, paramVal_10);
		paramMap.put(paramName_11, paramVal_11);
		paramMap.put(paramName_12, paramVal_12);
		paramMap.put(paramName_13, paramVal_13);
		paramMap.put(paramName_14, paramVal_14);
		paramMap.put(paramName_15, paramVal_15);
		paramMap.put(paramName_16, paramVal_16);
		paramMap.put(paramName_17, paramVal_17);
		paramMap.put(paramName_18, paramVal_18);
		paramMap.put(paramName_19, paramVal_19);
		paramMap.put(paramName_20, paramVal_20);
		paramMap.put(paramName_21, paramVal_21);
		paramMap.put(paramName_22, paramVal_22);
		paramMap.put(paramName_23, paramVal_23);
		paramMap.put(paramName_24, paramVal_24);
		paramMap.put(paramName_25, paramVal_25);
		
		
		for (Iterator<Map.Entry<String, String>> iterator = paramMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			String paramName = entry.getKey();
			String paramVal = entry.getValue();
			if (StringUtil.isBlank(paramName) 
					|| StringUtil.isBlank(paramVal)
					) {
				iterator.remove();
			}
		}
		
		try { 
			TFramedTransport transport = new TFramedTransport(new TSocket(ipAddress, Integer.valueOf(port), 30000)); 
			transport.open();
			TProtocol protocol = new TBinaryProtocol(transport); 
            ApiRequest.Client syncClient = new ApiRequest.Client(protocol); 
            
            iceResponse = syncClient.handleRequest(paramMap, ctxMap);
        } catch (Exception e) { 
        	iceResponse = e.getMessage();
        }
		
		model.addAttribute("iceResponse", iceResponse);
		return "bs/icetest/index";
	}
}